package com.cssw.fyzb.tps.api.adapay;

import com.huifu.adapay.core.AdapayCore;
import com.huifu.adapay.core.util.AdapaySign;
import com.ydn.appserver.Action;
import com.ydn.appserver.Request;
import com.ydn.appserver.Response;
import com.ydn.appserver.annotations.Function;
import com.ydn.appserver.annotations.Parameter;
import com.ydn.appserver.annotations.Type;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author: yangyiqun
 * @Date: 2021/1/06 22:29
 */
@Function(description = "校验签名", parameters = {
        @Parameter(name = "sign", type = Type.String, description = "签名", required = true),
        @Parameter(name = "data", type = Type.String, description = "数据", required = true)
})
@Component
public class VerifyAdapaySign implements Action {

    private static final Logger logger = LoggerFactory.getLogger(VerifyAdapaySign.class);

    @Override
    public Response execute(Request request) throws Exception {
        String sign = request.getString("sign");
        String data = request.getString("data");
        String publicKey = AdapayCore.PUBLIC_KEY;
        try {
            Map param = new HashMap();
            param.put("checkSign", AdapaySign.verifySign(data, sign, publicKey));
            return Response.success().put("data", param);
        } catch (Exception e) {
            logger.error("", e);
            return Response.fail("支付发起失败");
        }
    }
}
